package com.aaa.wash.controller;

import com.aaa.common.entity.CarWashRecord;
import com.aaa.common.entity.Station;
import com.aaa.common.util.BaseUtil;
import com.aaa.common.util.Result;
import com.aaa.common.vo.Page;
import com.aaa.wash.service.CarWashRecordService;
import com.aaa.wash.service.RemoteStationService;
import lombok.extern.log4j.Log4j2;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * (CarWashRecord)表控制层
 *
 * @author makejava
 * @since 2025-09-24 15:48:55
 */
@RestController
@RequestMapping("carWashRecord")
@Log4j2
public class CarWashRecordController {
    /**
     * 服务对象
     */
    @Resource
    private CarWashRecordService carWashRecordService;
    @Resource
    private RemoteStationService remoteStationService;

    @GetMapping("selectOne")
    public CarWashRecord selectOne(Integer orderId) {
        return this.carWashRecordService.selectOne(orderId);
    }

    @GetMapping("/selectCarWashRecord")
    public Result selectCarWashRecord(
            @RequestParam String carNo,
            @RequestParam Long storeId) {
        return BaseUtil.success(this.carWashRecordService.getLatestRecord(carNo, storeId));
    }

    @GetMapping("selectWashingCar")
    public Result selectWashingCar() {
        return BaseUtil.success(this.carWashRecordService.selectWashingCar());
    }

    @GetMapping("selectWashNumberByUserIdAndTime")
    public Result selectWashNumberByUserIdAndTime(@RequestParam(required = false) Integer userId,
                                                  @RequestParam(required = false) String monthFormat) {
        return BaseUtil.success(this.carWashRecordService.selectWashNumberByUserIdAndTime(userId, monthFormat));
    }

    @GetMapping("selectNumber")
    public Result selectNumber() {
        return BaseUtil.success(this.carWashRecordService.selectNumber());
    }

    /**
     * 通过店铺id查询该店铺的洗车次数
     *
     * @param storeId 店铺id
     * @return 查询结果
     */
    @GetMapping("selectWashNumberByStoreId")
    public Result selectWashNumberByStoreId(Integer storeId) {
        return BaseUtil.success(this.carWashRecordService.selectWashNumberByStoreId(storeId));
    }

    /**
     * 分页查询
     *
     * @param page 分页对象
     * @return 查询结果
     */
    @PostMapping("queryByPage")
    public Result queryByPage(@RequestBody Page<CarWashRecord> page) {
        return BaseUtil.success(this.carWashRecordService.queryByPage(page));
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("queryById")
    public Result queryById(Integer id) {
        return BaseUtil.success(this.carWashRecordService.queryById(id));
    }

    /**
     * 新增数据
     *
     * @param carWashRecord 实体
     * @return 新增结果
     */
    @PostMapping("add")
    public Result add(@RequestBody CarWashRecord carWashRecord) {

        return BaseUtil.success(this.carWashRecordService.insert(carWashRecord));
    }

    /**
     * 编辑数据
     *
     * @param carWashRecord 实体
     * @return 编辑结果
     */
    @PostMapping("edit")
    public Result edit(@RequestBody CarWashRecord carWashRecord) {
        List<CarWashRecord> carWashRecords = carWashRecordService.selectAll(carWashRecord.getStoreId());
//        不是新用户
        if (carWashRecords.size() > 1) {
            carWashRecord.setIsNew("1");
        } else {
//            是新用户
            carWashRecord.setIsNew("0");
        }
        carWashRecord.setWashStatus("1");
        Station station = new Station();
        if (carWashRecord.getProductId() == null) {
            station.setStation(carWashRecord.getStationId());
        } else {
            station.setStation(carWashRecord.getStation());
        }
        station.setIdle("0");
        remoteStationService.edit(station);
        return BaseUtil.success(this.carWashRecordService.update(carWashRecord));
    }

    /**
     * 删除数据
     *
     * @param id 主键
     * @return 删除是否成功
     */
    @DeleteMapping("deleteById")
    public Result deleteById(Integer id) {
        return BaseUtil.success(this.carWashRecordService.deleteById(id));
    }

}

